package Control;

import java.io.IOException;
import java.util.Iterator;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sun.xml.internal.bind.v2.schemagen.xmlschema.List;

import dao.ManterCddDAO;
import dto.Cdd;

import bo.ManterCddBO;

/**
 * Servlet implementation class ManterCddControl
 */
@WebServlet("/ManterCddControl")
public class ManterCddControl extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public ManterCddControl() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String tipoReq = request.getParameter("tipoRequisicao");
		ManterCddDAO dao = new ManterCddDAO();
		
		 if(tipoReq.equals("filtro")){
			
			String nome = request.getParameter("nomecdd");
			Iterator<Cdd> lista = dao.consultarCddPorNome(nome);
			
			request.setAttribute("listaCdd", lista);
			RequestDispatcher dispatcher = request.getRequestDispatcher("cdd/mantercdd.jsp");  
			dispatcher.forward(request, response);
			
		 }else if(tipoReq.equals("excluir")){
			 
			 int id = Integer.parseInt(request.getParameter("ID"));
			 
			 dao.excluirCdd(id);
			 Iterator<Cdd> lista = dao.consultarTodos();
				
			request.setAttribute("listaCdd", lista);
			RequestDispatcher dispatcher = request.getRequestDispatcher("cdd/mantercdd.jsp");  
			dispatcher.forward(request, response);
			 
		}else if(tipoReq.equals("alterar")){
			
			int id = Integer.parseInt(request.getParameter("ID"));
			System.out.println(id);
			Cdd cdd = dao.consultarCddPorId(id);
			System.out.println(cdd.getNomeCdd());
			request.setAttribute("cdd", cdd);
			RequestDispatcher dispatcher = request.getRequestDispatcher("cdd/editar.jsp");  
			dispatcher.forward(request, response);
			
		}else{
			Iterator<Cdd> lista = dao.consultarTodos();
			
			request.setAttribute("listaCdd", lista);
			RequestDispatcher dispatcher = request.getRequestDispatcher("cdd/mantercdd.jsp");  
			dispatcher.forward(request, response);  
		}
	}
	
	
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String tipoReq = request.getParameter("tipoRequisicao");
		ManterCddDAO dao = new ManterCddDAO();
		ManterCddBO bo = new ManterCddBO();
		
		if(tipoReq.equals("cadastro")){
			
			Cdd cdd = new Cdd();
			String nomeCdd = request.getParameter("nomecdd");
			cdd.setNomeCdd(nomeCdd);

			bo.validaInsercao(cdd);
			Iterator<Cdd> lista = dao.consultarTodos();
			
			request.setAttribute("listaCdd", lista);
			RequestDispatcher dispatcher = request.getRequestDispatcher("cdd/mantercdd.jsp");  
			dispatcher.forward(request, response);
			
		}else if(tipoReq.equals("alterar")){
			
			int id = Integer.parseInt(request.getParameter("ID"));
			String nome = request.getParameter("nomecdd");
			
			Cdd cdd = new Cdd();
			cdd.setIdCdd(id);
			cdd.setNomeCdd(nome);
			
			bo.validaAlteracao(cdd);
			Iterator<Cdd> lista = dao.consultarTodos();
			
			request.setAttribute("listaCdd", lista);
			RequestDispatcher dispatcher = request.getRequestDispatcher("cdd/mantercdd.jsp");  
			dispatcher.forward(request, response);
		}
	}

}
